﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ShareMarking.Entity;
using ShareMarking.Service;
using ShareMarking.Common;

namespace ShareMarking.Web
{
    /// <summary>
    /// GetUrlApi 的摘要说明
    /// </summary>
    public class GetUrlApi : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {// var method = ComLib.QueryStringHelper.GetFormString("action");
            string action = DTRequest.GetQueryString("action");
            switch (action)
            {
                case "geturl":
                    GetJumpUrl(context);
                    break;
                default:
                    context.Response.Write("{\"subcode\":\"03\",\"msg\":\"请求方法错误\",\"resulturl\":{}}");
                    break;
            }
        }
        private void GetJumpUrl(HttpContext context)
        {
            var code = ComLib.QueryStringHelper.GetFormString("code");
            var ip = ComLib.QueryStringHelper.GetFormString("ip");
            var ipadderss = ComLib.QueryStringHelper.GetFormString("ipadderss");
            try
            {
                ComLib.LogLib.Log4NetBase.Log("1、活动二维码请求到达code=" + code + "//ip=" + ip + "//Ipaddress=" + ipadderss);
                #region 开始认证
                if (code.IsNullOrEmpty())
                {
                    //context.Response.Write("请求参数错误");
                    context.Response.Write("{\"subcode\":\"02\",\"msg\":\"请求参数错误\",\"resulturl\":{}}");
                }
                else
                {
                    #region 检查code
                    qr_DiyCode qrdiy = new qr_DiyCode();
                    var iscode = qr_DiyCodeService.Instance.Get_IsExist_ByWhere(qr_DiyCode._.scene_id == code);
                    if (iscode)
                    {
                        var qrdiyentity = qr_DiyCodeService.Instance.Get_SingleEntity_byWhere(qr_DiyCode._.scene_id == code);
                        if (qrdiyentity != null)
                        {
                            #region 返回URL

                            var url = qrdiyentity.source_url;
                            if (url != "" && url != null)
                            {
                                results results = new Web.results();
                                results.subcode = "00";
                                results.msg = "成功";
                                results.resulturl = url;
                                string re = ComLib.JsonHelper.JsonSerializer<results>(results);
                                context.Response.Write(re);
                                ComLib.LogLib.Log4NetBase.Log("2、返回活动二维码成功！::::" + url);
                            }
                            #endregion

                            #region 添加访问记录
                            var ip2 = DTRequest.GetIP();//获取ip
                            var strip = "未知";
                            var stradderss = "未知";
                            if (ip.IsNullOrEmpty() || ipadderss.IsNullOrEmpty())
                            {
                                strip = "";
                                stradderss = "";
                                //context.Response.Write("{\"subcode\":\"02\",\"msg\":\"请求参数错误\",\"resulturl\":{}}");
                                ComLib.LogLib.Log4NetBase.Log("2、请求参数错误！ip或者ipadderss为空" + ip + ipadderss);
                                //return;
                            }
                            else
                            {
                                strip = ip;
                                stradderss = ipadderss;
                            }
                            qr_record recode = new qr_record()
                            {
                                qr_DiyCode_id = qrdiyentity.id,
                                Ip = strip,
                                Ipaddress = stradderss,
                                CreateOn = DateTime.Now
                            };
                            var result = qr_recordService.Instance.Add_Entity(recode);
                            if (result)
                            {
                                //ComLib.LogLib.Log4NetBase.Log("3、扫码记录添加成功::::qr_DiyCode_id：" + recode.qr_DiyCode_id + "//ip=" + recode.Ip + "//Ipaddress=" + recode.Ipaddress);
                            }
                            else
                            {
                                ComLib.LogLib.Log4NetBase.Log("3、扫码记录添加失败！::::qr_DiyCode_id：" + recode.qr_DiyCode_id + "//ip=" + recode.Ip + "//Ipaddress=" + recode.Ipaddress);
                            }
                            #endregion
                        }
                    }
                    else
                    {
                        context.Response.Write("{\"subcode\":\"01\",\"msg\":\"请求参数不存在！\",\"resulturl\":{}}");
                        ComLib.LogLib.Log4NetBase.Log("2、请求参数不存在！::::" + code);
                    }
                    #endregion
                }
                #endregion
            }
            catch (Exception ex)
            {
                ComLib.LogLib.Log4NetBase.Log("3.返回活动二维码失败！" + code + ex.Message + ex.StackTrace);
            }
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
    public class results
    {
        public string subcode;
        public string msg;
        public string resulturl;
    }
}